﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Web.Client.Model;
using SaaS.Core;
using XCore.Logger;

namespace Web.Client.Controllers
{
    /// <summary>
    /// 产品中心
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProjectController : ClientControllerBase
    {
     
        /// <summary>
        /// 获取所有已发布的产品
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<IList<ProjectModel>> List()
        {
            return SCC.Projects.Where(c => c.IsRelease).OrderByDescending(c => c.Sort)
                                     .Select(c => new ProjectModel(c.Id, c.Name, c.Description, c.Introductions))
                                     .ToList();
        }
        /// <summary>
        /// 获取产品明细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<ProjectDetailModel> Info(int id)
        {
            var project = SCC.Projects.Where(c => c.IsRelease && c.Id == id).Select(c => new { c.Id, c.Name }).FirstOrDefault();
            if (project == null) throw new MessageException(CoreSetting.Error_ID);
            var detail = SCC.ProjectDetails.Where(c => c.ProjectId == id).Select(c => new DetailModel(c.Id, c.Month, c.Price, c.Discount)).ToArray();
            return new ProjectDetailModel(project.Id, project.Name, detail);
        }
    }
}